﻿using AutoMapper;
using SimpleWebApi.Business.Service.Interface.DTO;
using SimpleWebApi.Business.Service.Interface.DTO.BusinessLicenseDTO;
using SimpleWebApi.MyMigration.Models;
using SimpleWebApi.MyMigration.Models.BusinessLicenseManagement;


namespace SimpleWebApi
{
    public class AutoMapConfig : Profile
    {
        public AutoMapConfig()
        {
            CreateMap<Commodity, CommodityDTO>().ForMember(c => c.CommodityId, s => s.MapFrom(c => c.Id))
                .ForMember(c => c.ProductId, s => s.MapFrom(c => c.ProductId))
                .ForMember(c => c.CategoryId, s => s.MapFrom(c => c.CategoryId))
                .ForMember(c => c.Title, s => s.MapFrom(c => c.Title))
                .ForMember(c => c.Price, s => s.MapFrom(c => c.Price))
                .ForMember(c => c.Url, s => s.MapFrom(c => c.Url))
                .ForMember(c => c.ImageUrl, s => s.MapFrom(c => c.ImageUrl));

            CreateMap<CompanyInfo, CompanyInfoDTO>();
            CreateMap<CommodityCreateDTO, Commodity>()
                // 忽略实体中的 Id（因为 DTO 中没有 Id）
                .ForMember(dest => dest.Id, opt => opt.Ignore());

            // Commodity -> CommodityCreateDTO（可选，按需配置）
            CreateMap<Commodity, CommodityCreateDTO>();

            // LegalPerson
            CreateMap<LegalPersonCreateDto, LegalPerson>()
                .ForMember(dest => dest.Id, opt => opt.Ignore())
                .ForMember(dest => dest.CreateTime, opt => opt.Ignore());
            CreateMap<LegalPerson, LegalPersonDto>();

            // RelatedPerson
            CreateMap<RelatedPersonCreateDto, RelatedPerson>();
            CreateMap<RelatedPerson, RelatedPersonDto>();

            // BusinessLicense
            CreateMap<BusinessLicenseCreateDto, BusinessLicense>()
                .ForMember(dest => dest.LegalPerson, opt => opt.Ignore()) // 手动处理
                .ForMember(dest => dest.RelatedPersons, opt => opt.Ignore());
            // 更新操作映射：BusinessLicenseUpdateDto -> BusinessLicense
            CreateMap<BusinessLicenseUpdateDto, BusinessLicense>()
                // 忽略无法自动映射的字段（如导航属性）
                .ForMember(dest => dest.LegalPerson, opt => opt.Ignore())
                .ForMember(dest => dest.RelatedPersons, opt => opt.Ignore())
                // 明确指定字段映射（可选，若属性名不一致）
                .ForMember(dest => dest.TradeName, opt => opt.MapFrom(src => src.TradeName))
                .ForMember(dest => dest.RegisteredCapital, opt => opt.MapFrom(src => src.RegisteredCapital))
                .ForMember(dest => dest.CompanyType, opt => opt.MapFrom(src => src.CompanyType))
                // 其他字段...
                .ForAllMembers(opts => opts.Condition((src, dest, srcMember) =>
                    srcMember != null && !srcMember.Equals(GetDefaultValue(srcMember.GetType()))));

            // 关联人更新 DTO -> 实体
            CreateMap<RelatedPersonUpdateDto, RelatedPerson>()
                .ForMember(dest => dest.Id, opt => opt.Ignore()) // 忽略 Id，防止主键被覆盖
                .ForMember(dest => dest.BusinessLicenses, opt => opt.Ignore()) // 忽略导航属性
                .ForAllMembers(opts => opts.Condition((src, dest, srcMember) =>
                    srcMember != null)); // 仅当源字段非空时更新
        }

        private static object GetDefaultValue(Type type)
        {
            return type.IsValueType ? Activator.CreateInstance(type) : null;
        }
    }
}